package com.hualan.sfzy.service.content.serviceimpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.content.Article;
import com.hualan.sfzy.entity.content.FM;
import com.hualan.sfzy.entity.content.FMClassify;
import com.hualan.sfzy.mapper.content.FMClassifyMapper;
import com.hualan.sfzy.mapper.content.FMMapper;
import com.hualan.sfzy.service.content.FMClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("fMClassifyService")
public class FMClassifyServiceImpl implements FMClassifyService {
    @Autowired
    private FMClassifyMapper mapper;
    @Autowired
    private FMMapper fmMapper;
    public  int add(FMClassify classify){
        return mapper.insert(classify);
    }
    public int remove(Integer id){
        FM fm=new FM();
        fm.setFmClassifyId(id);
        List<FM> list= fmMapper.select(fm);
        if(!list.isEmpty()){
            throw new RuntimeException("存在关联fm，不能删除");
        }
        return mapper.delete(id);
    }
    public int removeBatch(Integer[] ids){
        FM fm=new FM();
        for (Integer id : ids) {
            fm.setFmClassifyId(id);
            List<FM> list= fmMapper.select(fm);
            if(!list.isEmpty()){
                throw new RuntimeException("存在关联fm，不能删除");
            }
        }
        return mapper.deleteBatch(ids);
    }
    public int edit(FMClassify classify){
        return mapper.update(classify);
    }
    public FMClassify findById(Integer id){
        return mapper.selectById(id);
    }
    public List<FMClassify> find(){
        return mapper.select(null);
    }

    public Map<String,Object> findByPage(Integer pageNum, Integer pageSize, FMClassify classify){
        System.out.println(pageNum+" "+pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<FMClassify> list = mapper.select(classify);
        PageInfo<FMClassify> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;

    }
    
}
